package com.zyl.musicdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyl.musicdemo.R;
import com.zyl.musicdemo.activitys.SongListActivity;
import com.zyl.musicdemo.bean.AlbumBean;

import java.util.ArrayList;
import java.util.List;

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.AlbumItemViewHolder> {
    public static String ALBUM_NAME = "AlbumName";
    public static String SONG_BASES = "SongBeans";
    List<AlbumBean> albumBeans = new ArrayList();
    Context mContext;

    public AlbumAdapter(List<AlbumBean> albumBeans) {
        this.albumBeans = albumBeans;
    }

    @NonNull
    @Override
    public AlbumItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_item, parent, false);
        AlbumItemViewHolder viewHolder = new AlbumItemViewHolder(view);
        mContext = parent.getContext();
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final AlbumItemViewHolder holder, int position) {
        final AlbumBean albumBean = albumBeans.get(position);

        Glide.with(holder.itemView)
                .load(albumBean.getAlbumPoster())
                .into(holder.ivCover);
        holder.tvPlayNum.setText(albumBean.getPlayNum());
        holder.tvAlbumName.setText(albumBean.getAlbumName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //被点击时
                Intent intent = new Intent(mContext, SongListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList(SONG_BASES, new ArrayList(albumBean.getMusicModels()));
                bundle.putString(ALBUM_NAME, albumBean.getAlbumName());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return albumBeans.size();
    }

    class AlbumItemViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;
        TextView tvAlbumName, tvPlayNum;

        public AlbumItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_cover);
            tvAlbumName = itemView.findViewById(R.id.tv_album_name);
            tvPlayNum = itemView.findViewById(R.id.tv_play_num);
        }
    }
}
